package com.homework;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

public class MyPanel extends JPanel implements KeyListener, Runnable {
    Hero hero = null;
    Vector<EnemyTank> enemyTanks = new Vector<>();
    Vector<Boom> booms = new Vector<>();
    Vector<Node> nodes = null;
    private int enemySize = 6;
    Image boomimg = Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/img/boom.png"));//getResource获得绝对路径
    public MyPanel(int key) {
        switch (key) {
            case 1:
                new Thread(this).start();
                nodes = Recorder.readRecord();
                for (int i = 0; i < nodes.size(); i++) {
                    Node node = nodes.get(i);
                    if (i == nodes.size() - 1) {
                        hero = new Hero(node.getX(), node.getY(), node.getDirection());
                        break;
                    }
                    EnemyTank enemyTank = new EnemyTank(node.getX(), node.getY(), node.getDirection());
                    new Thread(enemyTank).start();
                    enemyTank.setEnemyTanks(enemyTanks);
                    enemyTanks.add(enemyTank);
                    enemyTank.newShot(enemyTank);
                }
                Recorder.setHero(hero);
                Recorder.setEnemyTanks(enemyTanks);
                break;
            case 2:
                new Thread(this).start();
                hero = new Hero(100, 100, 0);
                for (int i = 0; i < enemySize; i++) {
                    EnemyTank enemyTank = new EnemyTank(150 * (i + 1), 0, 2);
                    new Thread(enemyTank).start();
                    enemyTank.setEnemyTanks(enemyTanks);
                    enemyTanks.add(enemyTank);
                    enemyTank.newShot(enemyTank);
                }
                Recorder.setHero(hero);
                Recorder.setEnemyTanks(enemyTanks);
                break;
            default:
                System.out.println("输入有误");
                break;
        }

        //new PlayWave("music/stay.wav").start();
        new AudioPlayer("music/stay.wav").new AudioThread().start();
        //new AudioPlayer("music/stay.wav").getAudioThread().start();
    }

    public void showInfo(Graphics g) {
        g.setColor(Color.black);
        Font font = new Font("宋体", Font.BOLD, 25);
        g.setFont(font);
        g.drawString("累计击毁敌方坦克：", 1020, 30);
        drawTank(1020, 60, g, 0, 0);
        g.setColor(Color.black);
        //g.drawString(enemySize-enemyTanks.size()+"",1080,100);
        g.drawString(Recorder.getAllEnemyTankNum() + "", 1080, 100);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750);
        showInfo(g);
        if (hero != null && hero.isLive) {
            drawTank(hero.getX(), hero.getY(), g, hero.getDirection(), 1);
        }
        drawshot(g);
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < booms.size(); i++) {
            Boom o = booms.get(i);
            g.drawImage(boomimg, o.getX(), o.getY(), this);
            o.lifeDown();
            if (o.isLive == false) booms.remove(o);
        }
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank o = enemyTanks.get(i);
            if (o.shots.size() != 0) hitTank(o.shots, hero);
            if (hero.shots.size() != 0) {
                hitTank(hero.shots, o);
            }
            if (o.isLive) {
                drawTank(o.getX(), o.getY(), g, o.getDirection(), 0);
                for (int j = 0; j < o.shots.size(); j++) {
                    Shot shot = (Shot) o.shots.get(j);
                    if (shot.isLive) {
                        g.setColor(Color.blue);
                        g.fill3DRect(shot.getX(), shot.getY(), 5, 5, false);
                    } else {
                        o.shots.remove(shot);
                    }
                }
            }
        }
        this.repaint();
    }

    public void hitTank(Vector<Shot> shots, Tank enemyTank) {
        for (int i = 0; i < shots.size(); i++) {
            Shot s = shots.get(i);
            switch (enemyTank.getDirection()) {
                case 0:
                case 2:
                    if (s.getX() > enemyTank.getX() && s.getX() < enemyTank.getX() + 40
                            && s.getY() > enemyTank.getY() && s.getY() < enemyTank.getY() + 60) {
                        s.isLive = false;
                        enemyTank.isLive = false;
                        enemyTanks.remove(enemyTank);
                        shots.remove(s);
                        if (enemyTank == hero) hero.isLive = false;
                        booms.add(new Boom(enemyTank.getX(), enemyTank.getY()));
                        if (enemyTank instanceof EnemyTank) {
                            Recorder.addAllEnemyTankNum();
                            new AudioPlayer("music/blast.wav").new AudioThread().start();
                        }else {
                            new AudioPlayer("music/gameover.wav").new AudioThread().start();
                        }
                    }
                    break;
                case 1:
                case 3:
                    if (s.getX() > enemyTank.getX() && s.getX() < enemyTank.getX() + 60
                            && s.getY() > enemyTank.getY() && s.getY() < enemyTank.getY() + 40) {
                        s.isLive = false;
                        enemyTank.isLive = false;
                        enemyTanks.remove(enemyTank);
                        shots.remove(s);
                        if (enemyTank == hero) hero.isLive = false;
                        booms.add(new Boom(enemyTank.getX(), enemyTank.getY()));
                        if (enemyTank instanceof EnemyTank) {
                            Recorder.addAllEnemyTankNum();
                            new AudioPlayer("music/blast.wav").new AudioThread().start();
                        }else {
                            new AudioPlayer("music/gameover.wav").new AudioThread().start();
                        }
                    }
                    break;
            }
        }
    }

    public void drawshot(Graphics g) {
        for (int i = 0; i < hero.shots.size(); i++) {
            Shot shot = (Shot) hero.shots.get(i);
            g.setColor(Color.yellow);
            g.fill3DRect(shot.getX(), shot.getY(), 5, 5, false);
        }

    }

    public void drawTank(int x, int y, Graphics g, int direction, int type) {
        switch (type) {
            case 0:
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }
        switch (direction) {
            case 0://up
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 30, x + 20, y);
                break;
            case 1://right
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x + 30, y + 20, x + 60, y + 20);
                break;
            case 2://down
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 30, x + 20, y + 60);
                break;
            case 3://left
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x + 30, y + 20, x, y + 20);
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int speed = 15;
        if (e.getKeyCode() == KeyEvent.VK_W) {
            hero.setDirection(0);
            hero.moveUp();
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            hero.setDirection(1);
            hero.moveRight();
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            hero.setDirection(2);
            hero.moveDown();
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            hero.setDirection(3);
            hero.moveLeft();
        } else if (e.getKeyCode() == KeyEvent.VK_J) {
            hero.newShot(hero);
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true) {
            repaint();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
